package com.sparrow.common.findpath;

import com.sparrow.common.ai.findpath.GroupNode;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class MapPanel extends JPanel {

    public byte[][] mapData; // 存储地图数据的数组
    public static int tileSize = 50; // 瓦片大小
    public int[] start;
    public int[] end;
    private List<GroupNode> explores = new ArrayList<>();
    public List<int[]> paths = new ArrayList<>();

    public int pathStep;
    private int playStep;
    private Timer timer;

    public MapPanel(byte[][] mapData) {
        this.mapData = mapData;
        int width = mapData[0].length * tileSize; // 列数 -> x
        int height = mapData.length * tileSize;   // 行数 -> y
        setPreferredSize(new Dimension(width + 2, height + 2));
    }

    // 标记不可走点
    public void markBlock(int x, int y) {
        int g_x = x / tileSize;
        int g_y = y / tileSize;
        mapData[g_y][g_x] = 1; // ✅ y 行, x 列
    }

    public void markStart(int x, int y) {
        int g_x = x / tileSize;
        int g_y = y / tileSize;
        start = new int[] {g_x, g_y};
    }

    public void markEnd(int x, int y) {
        int g_x = x / tileSize;
        int g_y = y / tileSize;
        end = new int[] {g_x, g_y};
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // 绘制地图格子
        for (int y = 0; y < mapData.length; y++) {
            for (int x = 0; x < mapData[0].length; x++) {
                int drawX = x * tileSize;
                int drawY = y * tileSize;
                Graphics2D g2d = (Graphics2D) g;
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

                g.setColor(Color.BLACK);
                g.drawRect(drawX, drawY, tileSize, tileSize);

                if (mapData[y][x] == 1) { // 不可走点
                    g.setColor(Color.RED);
                    g.drawLine(drawX, drawY, drawX + tileSize, drawY + tileSize);
                    g.drawLine(drawX + tileSize, drawY, drawX, drawY + tileSize);
                }

                // 坐标信息
                g.setColor(Color.BLACK);
                g.drawString("(" + x + "," + y + ")", drawX + tileSize / 2 - 11, drawY + tileSize / 2);
            }
        }

        // 绘制探索过程
        for (int i = 1; i <= explores.size() && i <= playStep; i++) {
            GroupNode gn = explores.get(i - 1);
            int[] p = gn.getParent();
            g.setColor(Color.GREEN);
            g.drawRect(p[0] * tileSize, p[1] * tileSize, tileSize, tileSize);

            for (int[] n : gn.getNeighbors()) {
                g.setColor(Color.GRAY);
                g.fillRect(n[0] * tileSize, n[1] * tileSize, tileSize, tileSize);
            }
        }

        // 绘制起点和终点
        if (start != null) {
            g.setColor(Color.RED);
            g.fillRect(start[0] * tileSize, start[1] * tileSize, tileSize, tileSize);
        }
        if (end != null) {
            g.setColor(Color.RED);
            g.fillRect(end[0] * tileSize, end[1] * tileSize, tileSize, tileSize);
        }

        // 绘制路径线条
        for (int i = 0; i < paths.size() - 1 && i < pathStep; i++) {
            int[] a = paths.get(i);
            int[] b = paths.get(i + 1);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            g2d.setStroke(new BasicStroke(3.0f));
            g2d.setColor(Color.BLACK);

            int x1 = a[0] * tileSize + tileSize / 2;
            int y1 = a[1] * tileSize + tileSize / 2;
            int x2 = b[0] * tileSize + tileSize / 2;
            int y2 = b[1] * tileSize + tileSize / 2;
            g2d.drawLine(x1, y1, x2, y2);
        }

        // 绘制路径点
        for (int i = 0; i < paths.size(); i++) {
            int[] a = paths.get(i);
            int x3 = a[0] * tileSize + tileSize / 4;
            int y3 = a[1] * tileSize + tileSize / 4;

            Graphics2D g2d = (Graphics2D) g;
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            g2d.setColor(Color.GREEN);
            g2d.fillOval(x3, y3, tileSize / 2, tileSize / 2);
        }
    }

    public void play() {
        MapPanel mapPanel = this;
        mapPanel.playStep = 0;
        mapPanel.pathStep = 0;
        if (mapPanel.timer == null) {
            mapPanel.timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(() -> {
                        if (explores.size() > 0 && playStep < explores.size()) {
                            playStep++;
                        } else {
                            if (paths.size() > 0 && pathStep < paths.size()) {
                                pathStep++;
                            }
                        }
                        mapPanel.repaint();
                    });
                }
            });
            timer.start();
        }
    }

    public void setExplores(List<GroupNode> explores) {
        this.explores = explores;
    }

    public void setEnd(int[] end) {
        this.end = end;
    }

    public void setStart(int[] start) {
        this.start = start;
    }

    public void setPaths(List<int[]> paths) {
        this.paths = paths;
    }
}
